package cn.edu.ctbu.labtest.webapi;

import cn.edu.ctbu.labtest.core.PageUtils;
import cn.edu.ctbu.labtest.domain.Sc;
import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.helper.R;
import cn.edu.ctbu.labtest.helper.RUtil;
import cn.edu.ctbu.labtest.service.ScService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/sc")
public class ScRestController {

    @Autowired
    private ScService scService;

    @GetMapping("/findscbyname")
    public R<Sc> findScByName(String name, String order, Integer limit, Integer offset){
        Page<Sc> all;
        Pageable pageable = PageRequest.of(offset/limit,limit,
                Sort.by("id").descending());

        if(name==null||name.equals("")){
            all=scService.findAll(pageable);
        }
        else{
            all=scService.findScByNameLike("%"+name+"%",pageable);
        }
        return RUtil.success(all);
    }

    /**
     * 读取的url:/webapi/sc/getByPage
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getByPage")
    public PageUtils getByPage(@RequestParam(value ="page",defaultValue = "0")Integer page,
                               @RequestParam(value = "size",defaultValue = "10")Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name)
    {

        //按id排倒序
        Sort sort=Sort.by("id").descending();
        Page<Sc> scPage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable=PageRequest.of(page,size,sort);
            scPage=scService.findAll(pageable);
        }
        else{
            Sc sc=new Sc();
            sc.setName(name);

            Pageable pageable=PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询,即%{name}%
            Example<Sc> example=Example.of(sc,matcher);
            scPage=scService.findAll(example,pageable);
        }

        PageUtils pageUtils=new PageUtils(scPage.getContent(),scPage.getTotalElements());

        return  pageUtils;
    }

    /**
     * 老师查看
     * 读取的url:/webapi/sc/sclist
     * @return
     */
    @GetMapping("/sclist")
    public List<Sc> scList(){
        List<Sc> scs=scService.findAll();
        return scs;
    }

    /**
     * 学生查看
     * 读取的url:/webapi/sc/ssclist
     * @return
     */
    @GetMapping("/ssclist")
    public List<Sc> sscList(){
        List<Sc> scs=scService.findAll();
        return scs;
    }

    /**
     * 读取的url:/webapi/sc/findScByName
     */
    @GetMapping("/findScByName")
    public  List<Sc> findScByName(String name){
        List<Sc> all;
        if(StringUtils.isEmpty(name)){
            all=scService.findAll();
        }
        else{
            all=scService.findScByName(name);
        }
        return all;
    }

    /**
     * 读取的url:/webapi/sc/findScByNameLike
     */
    @GetMapping("/findScByNameLike")
    public  Page<Sc> findScByNameLike(String name,Pageable pageable){
        Page<Sc> all;
        if(StringUtils.isEmpty(name)){
            all=scService.findAll(pageable);
        }
        else{
            all=scService.findScByNameLike(name,pageable);
        }
        return all;
    }

    /**
     * 读取的url:/webapi/sc/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Sc get(@PathVariable Integer id){
        Sc sc=scService.getById(id);
        //sc.setPassword("");
        return sc;
    }

    /**
     * 新增学生
     *读取的url: /webapi/sc/insert
     */
    @PostMapping("/insert")
    public Sc insert(Sc sc){
        Sc sc1=scService.insert(sc);
        return sc1;
    }

    /**
     * 更新学生
     * 读取的url:/webapi/sc/update
     */
    @PutMapping("/update")
    public Sc update(Sc sc){
//        Sc oldSc=scService.getById(sc.getId());  //读取旧的数据
//        if(StringUtils.isEmpty(sc.getCname())){
//            sc.setCname(oldSc.getCname());
//        }
        Sc sc2=scService.insert(sc);
        return sc2;
    }

    /**
     * 删除学生成绩
     * 读取的url:/webapi/sc/delete
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        scService.delete(id);
    }

}








